/*
 * Created: Author: Aléx Carvalho / Date: Mar 16, 2015
 * 
 * Changed: $Author: ac280593@gmail.com $ / $Date: 2015-06-12 00:38:01 -0300 (Fri, 12 Jun 2015) $
 */
package com.cpm.view.zk.parts;

import com.cpm.view.CSSClass;
import com.cpm.view.Element;
import com.cpm.view.zk.MenuAction;
import java.util.Collections;
import java.util.Set;
import org.zkoss.zhtml.Div;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.HtmlBasedComponent;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;

public class MenuBar implements Element<HtmlBasedComponent>{

    private Set<MenuAction> menuItems = Collections.EMPTY_SET;
    private final org.zkoss.zul.Div content = new org.zkoss.zul.Div();
    private final Div rigth = new Div();

    public MenuBar() {
        initComponents();
    }

    public void clearItems() {
        rigth.getChildren().clear();
    }

    public void setMenuItems(Set<MenuAction> items) {
        this.menuItems = items;
    }

    public void updateLayout() {
        rigth.getChildren().clear();

        menuItems.forEach(a -> rigth.appendChild(getComponentMenuAction(a)));
    }
    
    private Component getComponentMenuAction(MenuAction action){
        Button button = new Button();
        button.setZclass(" ");
        button.setLabel(action.getLabel());
        button.addEventListener(Events.ON_CLICK, action.getClickListener() );
        button.setSclass(action.getIcon().getIconClass());
        button.setHeight(MenuAction.ITEM_HEIGHT + "px");
//        button.setWidth(MenuAction.ITEM_WIDTH + "px");
        button.setTooltiptext(action.getTooltip());
        button.setStyle("border: none; background-color: transparent;border-right: 1px rgb(163, 162, 162) solid;white-space: nowrap;");
        
        return button;
    }

    private void initComponents() {

        content.setId( content.getUuid() );
        
        rigth.setSclass("pull-right");
        rigth.setStyle("padding: 4px 0px 0px 0px; max-width: 50%; display: flex;");

        content.setSclass(CSSClass.MENU_BAR.toString());
        content.appendChild(rigth);
    }

    @Override
    public HtmlBasedComponent getElement() {
        return content;
    }
}
